package cn.edu.sgu.www.mhxysy.controller.gang;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.gang.GangDTO;
import cn.edu.sgu.www.mhxysy.entity.gang.Gang;
import cn.edu.sgu.www.mhxysy.pager.gang.GangPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.gang.GangService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "帮派管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/gang", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class GangController {

	private final GangService gangService;

	@Autowired
	public GangController(GangService gangService) {
		this.gangService = gangService;
	}

	@ApiOperation("添加帮派")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated GangDTO gangDTO) {
		gangService.insert(gangDTO);

		return JsonResult.success(CustomMessages.INSERT_SUCCESS);
	}

	@ApiOperation("通过ID删除帮派")
	@RequestMapping(value = "/deleteById", method = RequestMethod.POST)
	public JsonResult<Void> deleteById(
			@ApiParam(name = "id", value = "帮派ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
		gangService.deleteById(id);

		return JsonResult.success(CustomMessages.DELETE_SUCCESS);
	}

	@ApiOperation("通过ID修改帮派信息")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated GangDTO gangDTO) {
		gangService.updateById(gangDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("查询全部帮派")
	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResult<List<Gang>> selectAll() {
		List<Gang> list = gangService.selectAll();

		return JsonResult.success(null, list);
	}

	@ApiOperation("通过ID查询帮派信息")
	@RequestMapping(value = "/selectById", method = RequestMethod.GET)
	public JsonResult<Gang> selectById(
			@ApiParam(name = "id", value = "帮派ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
		Gang gang = gangService.selectById(id);

		return JsonResult.success(null, gang);
	}

	@ApiOperation("分页查询帮派列表")
	@RequestMapping(value = "/selectByPage", method = RequestMethod.GET)
	public JsonResult<JsonPage<Gang>> selectByPage(GangPager pager) {
		Page<Gang> page = gangService.selectByPage(pager);

		return JsonResult.restPage(page);
	}

}